﻿/* 
 * Godtear.com
 * QQ:bzure@189.cn
 *Author:Nathan Liu
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Bzure.Common.Core;

namespace Bzure.Security.Permissions
{
    /// <summary>
    /// 角色名必须唯一
    /// </summary>
    [Serializable ]
    public class TreeRole : DroitCollection, ISecurityRole
    {
        public TreeRole(string  id, string roleName)
        {
            ID = id;
            _name = roleName;
        }
        #region Implement ISecurityRole interface
        public string  ID
        {
            get;
            private set;
        }
        /// <summary>
        /// 角色类型
        /// </summary>
        public RoleType RoleType { get { return RoleType.TreeRole; } }
        /// <summary>
        /// 角色描述
        /// </summary>
        public string RoleDescription { get; set; }
        private string _name;
        public string Name
        {
            get { return _name; }
        }
        #endregion Implement ISecurityRole interface

        #region Implement ITree interface
        public TreeRole _parent;
        public TreeRole Parent
        {
            get { return _parent; }
        }
        #endregion Implement ITree interface
        #region IPersistenceState
        bool _isPersisted = false;
        public bool IsPersisted()
        {
            return _isPersisted;
        }
        public IPersistenceState MarkPersistenceState(bool IsPersisted)
        {
            _isPersisted = IsPersisted;
            return this;
        }
        #endregion
        private void Recurse(TreeRole tree)
        {
            this.AddDroit(tree.ColligateDroits());
        }
        public virtual IEnumerable<IDroit> ColligateDroits()
        {
            Recurse(this._parent);
            return this.DriotCollection;
        }
        public virtual bool IsInRole(string  roleID)
        {
            return this.ID == roleID || this.Parent.IsInRole(roleID);
        }
        
        public override bool Equals(object obj)
        {
            if (obj is TreeRole)
            {
                TreeRole oTree = obj as TreeRole;
                return this.ID.Equals(oTree.ID);
            }
            return base.Equals(obj);
        }
        public override int GetHashCode()
        {
            return this.ID .GetHashCode();
        }
    }
}
